Const kITEM_BOMB=0
Const kITEM_SPEED=1
Const kITEM_MISSIL=2

Type TItem
	Field m ; mesh
	Field cat; categorie 0=Bomb, 1=Speed Bonus, 2=missil 
	Field X#,Y#,Z# ; position in 3d-space
 	Field DisplayTimeLive.Timer
End Type 


Function TItem_CreateBomb.TItem( pX#, pY#, pZ#)
	Local l.TItem = New TItem
	l\m = CreateSphere(5)
	PositionEntity l\m, pX#, pY#, pZ#
	Cel_Shade( l\m,0.5,0.5,.5,0.05)
	EntityColor l\m, 84,84,84	
	
	
	l\cat = kITEM_BOMB
	l\DisplayTimeLive = SetTimer( Rand(4000, 6000) )
	Return l
End Function	

Function TItem_CreateSpeed.TItem( pX#, pY#, pZ#)
	Local l.TItem = New TItem
	l\m = CreatePillMesh(1,1.2,3)
	Cel_Shade( l\m,0.5,0.5,.5,0.05)
	PositionEntity l\m, pX#, pY#, pZ#
	EntityColor l\m, 255,255,0
	l\cat = kITEM_SPEED
	l\DisplayTimeLive = SetTimer( Rand(4000, 6000) )
	Return l
End Function	

Function TItem_CreateMissil.TItem( pX#, pY#, pZ#)
	Local l.TItem = New TItem
	l\m = CopyEntity(g_App\TEMPLATE_MISSIL) 
	PositionEntity l\m, pX#, pY#, pZ#
	l\cat = kITEM_MISSIL
	l\DisplayTimeLive = SetTimer( Rand(5000, 7000) )
	Return l
End Function	


Function TItem_generator(p_factor=50, p_randMax=1000)
	Local cat,wp_index
	Local wp.TWayPoint
	If Rand( 1, p_randMax) < p_factor Then
		cat = Rand(kITEM_BOMB,kITEM_MISSIL)
		wp_index=Rand(1,TWayPoint_Count() )
		wp.TWayPoint = TWayPoint_Get(wp_index)
		If cat=kITEM_BOMB Then TItem_CreateBomb(EntityX(wp\mesh)+Rand(-4,4), 1, EntityZ(wp\mesh)+Rand(-4,4))
		If cat=kITEM_SPEED Then TItem_CreateSpeed(EntityX(wp\mesh)+Rand(-4,4), 1, EntityZ(wp\mesh)+Rand(-4,4))
		If cat=kITEM_MISSIL Then TItem_CreateMissil(EntityX(wp\mesh)+Rand(-4,4), 1, EntityZ(wp\mesh)+Rand(-4,4))
	End If
End Function

Function TItem_Update()
	TItem_generator()
	
	Local c.TCar=Null
	Local i.TItem=Null
	For i.TItem = Each TItem
		; operation about display
		If TimeOut(i\DisplayTimeLive)
			If i\cat=kITEM_BOMB And EntityInView( i\m,g_App\Cam1)
				TExplosion_Create( i\m,1 )
			EndIf 	
			TItem_Free( i)
		Else
			
			If i\cat = kITEM_MISSIL 
				TurnEntity i\m, 0,3,0
			EndIf 
			
			For c = Each TCar
				; collision between item and a car
				If EntityDistance( i\m, c\mesh ) < 2
					If i\cat=kITEM_BOMB
						If EntityInView( i\m,g_App\Cam1)
							TExplosion_Create( i\m,1 )
							TExplosion_Create( c\mesh,1 )
						EndIf 
						c\bExploded=True
						c\WaitExplodeTime=SetTimer(2000)
					ElseIf i\cat=kITEM_SPEED And c\bSpeed = False 
						c\bSpeed = True
						c\WaitSpeedTime=SetTimer(4000)
						c\speed#=c\speed#+0.005
					ElseIf i\cat=kITEM_MISSIL And c\bMissil = False
						c\bMissil = True
					EndIf  
					
					TItem_Free( i )
					Exit
				EndIf 
			Next
		EndIf 
		
	Next 
	
End Function 


Function TItem_Free( p_I.TItem )
	FreeEntity(p_I\m)
	TimeOut(p_I\DisplayTimeLive)
	Delete p_I
End Function	


Function CreatePillMesh(rad# = 1, height# = 2, seg = 8)
	mesh = CreateCylinder(seg * 2)
	ScaleMesh mesh, rad#, height# * .6, rad#
	c = CreateSphere(seg)
	ScaleMesh c, rad#, rad#, rad#
	PositionMesh c, 0, height# * .5, 0
	AddMesh c, mesh
	FreeEntity c
	c = CreateSphere(seg)
	ScaleMesh c, rad#, rad#, rad#
	PositionMesh c, 0, -height# * .5, 0
	AddMesh c, mesh
	FreeEntity c
	Return mesh
End Function

;~IDEal Editor Parameters:
;~B#44#52#53
;~C#Blitz3D